package AdminTab;


import java.io.File;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class myHybridFramework {
	String[][] xTD, xTC, xTS; //2 dimensional variable to read/Store the test data
	String vKW, vIP1, vIP2, vtestData;  //Declaring inputs from spreadsheet
	String dURL,	dilogin, dipaswd, dqinput,	dExecute, dExpectedResults,	dActualResults;
	WebDriver myD ;
	String xlPath, xlResPath; //Excel path
	String vError, vResult, vScreenshot, vTempRes;//Declaring the error columns on the excel

	
	@Test
	public void myDriverscript() throws Exception{

	xlPath = "C:\\Priya\\Silverback 4.4\\Automation Test Cases\\Admin\\DevicePrivacyControls\\DevicePrivacyControls.xls";
	xlResPath = "C:\\Priya\\Silverback 4.4\\Automation Test Cases\\Admin\\";
		
	//Read the entire TC and TS
	xTC = xlReadWrite.xlRead(xlPath, "TestCases");
	xTS = xlReadWrite.xlRead(xlPath, "TestSteps");
	xTD = xlReadWrite.xlRead(xlPath, "TestData");
	
	for (int h=1; h<xTD.length; h++){  //Go to each row in TD
		if (xTD[h][4].equalsIgnoreCase("Y")){ //is the test Data execution for yes
			//Then read the values of the following local variables data from each row
		dURL =xTD[h][1];
		dilogin = xTD[h][2];
		dipaswd = xTD[h][3];
		dqinput = xTD[h][4];
	
	for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
		if (xTC[i][3].equalsIgnoreCase("Y")) {	//go to every test step if it is y
			xTC[i][4] = "Pass";  //TC is pass by default on the excel, it updates the excel if the test case/step fails
			for (int j=1; j<xTS.length; j++){ 
			if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
				
				System.out.println("KW to execute is " + xTS[j][4]);
				System.out.println("IP1 to execute is " + xTS[j][5]);
				System.out.println("IP2 to execute is " + xTS[j][6]);
				
				vError = "Pass";
				vResult = "-";
				vScreenshot = "-";
				vtestData = getTDValue(vtestData);
					
			try{
				KWexecutor.vKW = xTS[j][4] ;						
				KWexecutor.vIP1 = xTS[j][5];
				KWexecutor.vIP2 = xTS[j][6];
				
			
				vTempRes = KWexecutor.call_KW(); // call the corresponding KW function		
					
			} catch (Exception mye){ //Unexpected error from the Keyword Executor method
				vResult = "Fail";
				vError = "Error is " + mye;
				vScreenshot = "xlResPath" + xTS[j][0] + "-" + xTS[j][1] + ".png";
				File scrFile = ((TakesScreenshot)myD).getScreenshotAs(OutputType.FILE);	//code to capture screenshot			
				FileUtils.copyFile(scrFile, new File(vScreenshot));
				
				System.out.println("Step failed:" + vError);
				xTC[i][4] = "Fail";	
				
				
				
			}
				if(vTempRes.equalsIgnoreCase("Fail")) { //Expected Verify error from Keyword Executor method
				   vResult = "Fail";
				   vError = "Verify Error Occured:" + vKW;
				 //vScreenshot = myD.
				   System.out.println("Step failed:" + vError);
				   xTC[i][4] = "Fail";	
				   vScreenshot = "xlResPath" + xTS[j][0] + "-" + xTS[j][1] + ".png";
				   File scrFile = ((TakesScreenshot)myD).getScreenshotAs(OutputType.FILE);	//code to capture screenshot			
					FileUtils.copyFile(scrFile, new File(vScreenshot));
				}
				
				xTS[j][8] = vResult;
				xTS[j][9] = vError;
				xTS[j][10] = vScreenshot;
				
				}
			}

			}
		//Write back the test Steps with the error
		xlReadWrite.writeXL("C:/Priya/Silverback 4.4/Automation Test Cases/Admin/DevicePrivacyControls/Results/TestCase-"+xTC[i][0]+".xls", "TestCase Results", xTC );
		xlReadWrite.writeXL("C:/Priya/Silverback 4.4/Automation Test Cases/Admin/DevicePrivacyControls/Results/TestStep-"+xTS[i][0]+".xls", "TestSteps Results", xTS );
		
	
		}
	
	}
}
	
System.out.println("Test Run Complete");

}	




private String getTDValue(String vTestData) {  

if (vTestData.equals("dURL")){
	return dURL;  //returns the value in it
}
if (vTestData.equals("dilogin")){
	return dilogin;
}
if (vTestData.equals("dipaswd")){
	return dipaswd;
}
if (vTestData.equals("dqinput")){
	return dqinput;

}

			
return vTestData;  //If no data found , then it takes the vTestData as is in the Test Steps sheet
}
}





